package cn.comtom.ewbs.signature.common.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import cn.dev33.satoken.interceptor.SaInterceptor;


/**     
 * @Description:WebMvc配置 继承了WebMvcConfigurationSupport类，实现WebMvcConfigurer接口就会不生效
 * @author: liuhy
 * @date:   2021/04/13 
 *   
 * @Copyright: 2021 www.comtom.cn Inc. All rights reserved. 
 */
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
	
    @Override
	public void addInterceptors(InterceptorRegistry registry) {
    	// 注册拦截器
    	LoginPageHandler loginInterceptor = new LoginPageHandler();
    	registry.addInterceptor(loginInterceptor)
                 // 拦截路径
                .addPathPatterns("/**").excludePathPatterns("/static/**","/sign/*","/open/**","/**/doc.html","/webjars/**","/swagger-resources/**","/v2/api-docs/**","/favicon.ico");
    	
        // 注册Sa-Token的路由拦截器
        //registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
       //     .addPathPatterns("/**")
       //     .excludePathPatterns("/static/**","/sign/*","/open/**","/**/doc.html","/webjars/**","/swagger-resources/**","/v2/api-docs/**","/favicon.ico"); //除了/open/**以外的所有接口都需要登录才能访问
       
        // 注册Sa-Token的路由拦截器，拦截是否有权限访问接口
        registry.addInterceptor(new SaInterceptor(handle -> {
        	// 查询所有的接口地址及接口权限标识
            //SaRouter.match(per.getPath(), r -> StpUtil.checkPermission(per.getPerms()));
        }));
	}


	/**
     * 页面跨域访问Controller过滤
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        WebMvcConfigurer.super.addCorsMappings(registry);
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("POST", "GET", "PUT", "DELETE")
                .allowedOrigins("*");
    }


    /**
     * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。 需要重新指定静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:static/","classpath:META-IFA/resources/","classpath:resources/","classpath:public/","classpath:/");
        registry.addResourceHandler("doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/**/*.html").addResourceLocations(
            "classpath:/META-INF/resources/");
        registry.addResourceHandler("/monitor/**").addResourceLocations(
            "classpath:/META-INF/resources/monitor/");
    }


    /**
     * 配置servlet处理
     */
    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }


}

